<?php

class Users extends CI_Controller {


	var $page  = 'users';
	var $action  = '';
	var $hasPagination = true;
	var $currentPage = 0;

	public function index(){

		$data['usertype'] = 'Client';
		$data['warehouseList'] = $this->getAllWarehouse();
			
		$perPage = 20;
			
		$allClients = $this->db->query("select u.*,
			    (select concat(firstname,' ',lastname) from users usr where u.updatedby = usr.id) as updatedby
				 from users u
				where u.usertype = '".CLIENT."' and u.valid = 1");
			
			
			
			
		if($this->hasPagination){

			$config['base_url'] = ADMINBASEURL.'/users/paging/';
			$config['total_rows'] = $allClients->num_rows();
			$config['per_page'] = 3;
			$data['paginationLinks'] = $this->generatePaginationLinks($config);

			$start = 1;
			$ending = 20;

			if(isset($this->currentPage) && $this->currentPage > 1){
				$start = ($this->currentPage * $perPage ) -  $perPage;
				$ending = $start + $perPage;
						 			}
						 			//echo "select * from users where usertype = '".CLIENT."' limit  $start , $ending";
						 			$query = $this->db->query("select * from users where usertype = '".CLIENT."' and valid =1  limit  $start , $perPage");



		}
			
		$this->admin($data,$query);
			
	}

	public function paging($currentPage){
		$this->currentPage = $currentPage;
		$this->index();
	}





	public function add(){
		$this->action = 'add';
		$this->index();
	}

	public function view($usertype){

		$query = $this->db->query("select * from users where usertype = '".$this->equivUsertype($usertype)."' and valid = 1");
			
		$data['usertype'] = ucwords($usertype);
			
		$data['warehouseList'] = $this->getAllWarehouse();
			
		$this->admin($data,$query);
	}



	public function delete($id){

		$data['deleteId'] = $id;

		$user = $this->db->query("select * from users where id = $id and valid = 1 limit 1" );
			
		if($user->row()!= null){
			$temp = $user->row();
			$this->db->query("delete from users where id = $id and valid = 1 limit 1" );
			$query = $this->db->query("select * from users where usertype = '".$temp->usertype." and valid = 1'");
			$data['usertype'] = ucwords($temp->usertype);
			$data['warehouseList'] = $this->getAllWarehouse();
			$data['successMessage'] = "Record was successfully deleted";

			$this->createLog('USERS' , 'DELETED' ,  "Deleted User Record where id is $id");

			$this->admin($data,$query);


		}else
			
		$this->index();
			
	}

	public function edit($id){
			
		//$this->page = 'edit'.$this->page;
			
		$this->action = 'edit';
			
		$query = $this->db->query("select * from users where id = $id and valid= 1 limit 1" );
		//$row =
		$data['user'] = $query->row();

			
		$data['transactionList'] = $this->transactionListByUser( $query->row()->id ,null ,null);
			
		$data['warehouseList'] = $this->getAllWarehouse();
			
		$query = $this->db->query("select * from users where usertype = '".$this->equivUsertype($query->row()->usertype)."' and valid = 1");
			
		$query= null;

			
		$this->admin($data,$query);
			
	}

	public function save(){
			
		$firstname = $_REQUEST['firstname'];
		$lastname =  $_REQUEST['lastname'];
		$email =  $_REQUEST['email'];
		$middlename =  $_REQUEST['middlename'];
		//	$warehouse =  $_REQUEST['warehouse'];
		$warehouse =  '';
		$usertype =  $_REQUEST['usertype'];
		$username = $_REQUEST['username'];
		$password = $_REQUEST['password'];
		$address = $_REQUEST['address'];
		$companyname = $_REQUEST['companyname'];

		if($this->validateFields() == FALSE){
			$data['errorMessage'] = validation_errors();
			$this->admin($data, null);
				return;
		}


		$emailfornewuser = false;
		$emailfornewtransaction = false;
		$emailforsuccessfuldelivery = false;

		$type = "";
		$str="";

		if(isset($_REQUEST['type']))
		$type = $_REQUEST['type'];

		$data = "";


		if(isset( $_REQUEST['emailfornewuser']))
		$emailfornewuser = true;

		if(isset( $_REQUEST['emailfornewtransaction']))
		$emailfornewtransaction = true;

		if(isset( $_REQUEST['emailforsuccessfuldelivery']))
		$emailforsuccessfuldelivery = true;

			

			

		if(isset( $_REQUEST['id'])){
			$str = "UPDATE users SET usertype='$usertype',firstname='$firstname',lastname='$lastname',username='$username',email='$email',updatedon=NOW(),warehouse='$warehouse'
					,password='$password',middlename='$middlename',address ='$address',type='$type',
					emailfornewuser = '$emailfornewuser' ,emailfornewtransaction = '$emailfornewtransaction',
				    emailforsuccessfuldelivery = '$emailforsuccessfuldelivery', companyname = '$companyname'
				    where id=". $_REQUEST['id'];

			$this->db->query($str);
			$data['successMessage'] ="Record was successfully Updated.";

			$this->createLog('USERS' , 'CREATED' ,  "Updated User where id = ".$_REQUEST['id']);

		}else{
			$str = "INSERT INTO users( usertype, createdon, firstname, middlename, lastname, username, email, updatedon, warehouse, password , type,emailfornewuser ,emailfornewtransaction , emailforsuccessfuldelivery ,referencenumber,address , valid , companyname) ".
			$str = $str ."VALUES ('$usertype',NOW(),'$firstname','$middlename','$lastname','$username','$email',NOW(),'$warehouse','$password','$type' , '$emailfornewuser' ,'$emailfornewtransaction' , '$emailforsuccessfuldelivery' , 'SLC-".time()."','$address' , 1 , '$companyname')";

			$this->db->query($str);
			$data['successMessage'] = "New Record was successfully Created.";
			
			
			$k = $this->db->query("select * from users where id = ".($this->db->insert_id())." and valid= 1 limit 1" );
			
			if(count($k) > 0){
				$details['user'] = $k->row();
				$details['receipients'] = array($details['user']->email);  //$details['user']->email;
				$this->sendEmail($emailMessageTemplate,  $details ,   "Your Account in Skybest was Successfully Created");
			
			}

			$this->createLog('USERS' , 'CREATED' ,  "Created new User");
		}



		$query = $this->db->query("select * from users where usertype = '$usertype'");
			

		$this->admin($data,$query);

	}



	public function equivUsertype($usertype){
		if($usertype == 'admin')
		return ADMIN;
		else if($usertype == 'staff')
		return STAFF;
		else if($usertype == 'client')
		return CLIENT;
		else if($usertype == 'manager')
		return MANAGER;
	}

	public function getAllWarehouse(){

		return $this->db->query("select * from warehouse");
			
	}






}
